<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="addRole"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table border="" :data="roleList">
              <el-table-column label="序号" width="120" type="index" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template #default="obj">
                  <el-button size="small" type="success" @click="assignPerm(obj.row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="editRole(obj.row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="deleteRole(obj.row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination layout="prev,pager,next" :page-size="pageSetting.pagesize" :total="total" @current-change="currentChange" />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="公司名称">
                <el-input v-model="companyData.name" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="companyData.companyAddress" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="companyData.mailbox" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="companyData.remarks" type="textarea" :rows="3" disabled style="width:400px" />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <!-- 新增编辑弹出层 -->
      <el-dialog title="编辑弹层" :visible="showDialog" @close="btnCancel">
        <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="120px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" @click="btnCancel">取消</el-button>
            <el-button size="small" type="primary" @click="btnOK">确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>
      <!-- 分配权限弹出层 -->
      <el-dialog title="分配权限" :visible="showPermDialog" @close="btnPermCancel">
        <!-- 权限是一颗树 -->
        <!-- 将数据绑定到组件上 -->
        <!-- check-strictly 如果为true 那表示父子勾选时  不互相关联 如果为false就互相关联 -->
        <!-- id作为唯一标识 -->
        <el-tree
          ref="permTree"
          :data="permData"
          :props="defaultProps"
          :show-checkbox="true"
          :check-strictly="true"
          :default-expand-all="true"
          :default-checked-keys="selectCheck"
          node-key="id"
        />
        <!-- 确定 取消 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button type="primary" size="small" @click="btnPermOK">确定</el-button>
            <el-button size="small" @click="btnPermCancel">取消</el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getCompanyInfo, getRoleList, deleteRole, addRole, getRoleDetail, updateRole, assignPerm } from '@/api/setting'
import { getPermissionList } from '@/api/permisson'
import { ListToTreeData } from '@/utils/index'
export default {
  data() {
    return {
      companyData: {}, // 公司信息
      roleList: [], // 角色列表
      pageSetting: {
        page: 1, // 第几页
        pagesize: 2 // 每页渲染几条
      },
      total: 0, // 总页数
      showDialog: false,
      // 专门接收新增或者编辑的编辑的表单数据
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '角色描述不能为空', trigger: 'blur' }]
      },
      showPermDialog: false, // 控制分配权限弹层的显示后者隐藏
      defaultProps: {
        label: 'name'
      },
      permData: [], // 专门用来接收权限数据 树形数据
      selectCheck: [], // 定义一个数组来接收 已经选中的节点
      roleId: null // 用来记录分配角色的id
    }
  },
  created() {
    this.getCompanyInfo()
    this.getRoleList()
  },
  methods: {
    // 获取公司信息
    async getCompanyInfo() {
      this.companyData = await getCompanyInfo(this.$store.state.user.userInfo.companyId)
    },
    // 获取角色列表
    async getRoleList() {
      const { rows, total } = await getRoleList(this.pageSetting)
      this.roleList = rows
      this.total = total
    },
    // 分页发生改变
    currentChange(page) {
      this.pageSetting.page = page
      this.getRoleList()
    },
    // 删除
    async deleteRole(id) {
      await this.$confirm('您确定要删除吗?')
      await deleteRole(id)
      this.$message.success('删除成功')
      if (this.total % this.pageSetting.pagesize === 1 && this.pageSetting.page === Math.ceil(this.total / this.pageSetting.pagesize)) {
        this.pageSetting.page--
      }
      this.getRoleList()
    },
    // 新增按钮
    addRole() {
      this.showDialog = true
    },
    // 点击编辑
    async editRole(id) {
      this.showDialog = true
      this.roleForm = await getRoleDetail(id)
    },
    // 新增编辑取消
    btnCancel() {
      this.$refs.roleForm.resetFields()
      this.roleForm = {}
      this.showDialog = false
    },
    // 新增编辑确定
    async btnOK() {
      try {
        if (this.roleForm.id) {
          await this.$refs.roleForm.validate()
          await updateRole(this.roleForm)
          this.$message.success('编辑成功')
          this.btnCancel()
          this.getRoleList()
        } else {
          await this.$refs.roleForm.validate()
          await addRole(this.roleForm)
          this.$message.success('新增成功')
          this.btnCancel()
          this.getRoleList()
        }
      } catch (err) {
        console.log(err)
      }
    },
    // 分配权限
    async assignPerm(id) {
      this.roleId = id
      this.permData = ListToTreeData(await getPermissionList(), '0')
      const { permIds } = await getRoleDetail(id)
      this.selectCheck = permIds
      this.showPermDialog = true
    },
    // 分配权限取消
    btnPermCancel() {
      this.showPermDialog = false
      this.selectCheck = []
    },
    // 分配权限确定
    async btnPermOK() {
      console.log(this.$refs.permTree.getCheckedKeys())
      await assignPerm({ id: this.roleId, permIds: this.$refs.permTree.getCheckedKeys() })
      this.$message.success('操作成功')
      this.btnPermCancel()
    }
  }
}
</script>

<style>

</style>
